<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/19
 * Time: 15:46
 */

namespace app\api\v1\controller;

use think\Controller;
use think\Request;
use think\Config;
use lmxdawn\jwt\JWT;

class JwtApi extends Controller
{
    /**
     * JwtApi constructor.
     */
    public function __construct()
    {
        Config::load(APP_PATH . 'api/v1/config.php');
    }

    /**
     * 获取jwt
     * @param Request $request
     * @return \think\response\Json
     */
    public function getJwt(Request $request)
    {
        $username = $request->param('username');

        $sub = [
            'username' => $username,
        ];

        $key = config('key');
        $time = time();
        $token = array(
            "iss" => config('iss'),//该JWT的签发者
            "aud" => config('aud'),//接收该JWT的一方
            "sub" => $sub,//该JWT所面向的用户
            "iat" => $time,//在什么时候签发的
            "exp" => $time+120,// 什么时候过期，这里是一个Unix时间戳
        );
        $jwt = JWT::encode($token, $key,'HS256');
        return json($jwt);
    }
}